#! /usr/bin/env python
# -*- coding: utf-8 -*-

"""
html解析测试 - css选择器
"""

import re
from bs4 import BeautifulSoup

html_doc = """
<html><head><title>The Dormouse's story - 测试</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>
    ddd
<a href="http://example.com/lacie" class="sister1" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister2" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
<span>中文</span>
"""



def main():
    """
    测试
    :return:
    """
    soup = BeautifulSoup(html_doc, "lxml")
    print(type(soup))

    print('*' * 40)
    # 通过tag标签逐层查找
    print(soup.select('body a'))
    # 查找某个tag标签下的直接子标签
    print(soup.select('head > title'))

    print('*' * 40)
    # 查找id为link1的兄弟节点
    print(soup.select('#link1 ~ *'))

    print('*' * 40)
    # 通过css类名查找
    print(soup.select('.title'))

    print('*' * 40)
    # 通过tag的id查找
    print(soup.select('#link1'))

    print('*' * 40)
    # 通过是否存在某个属性来查找
    print(soup.select('a[href]'))

    # 模糊匹配
    print(soup.select('a[href*=".com/el"]'))


if __name__ == '__main__':
    main()